package com.mall.controller;

import com.mall.annotation.AdminLimit;
import com.mall.annotation.LoginToken;
import com.mall.annotation.MangerLimit;
import com.mall.dto.ActionResult;
import com.mall.po.Goods;
import com.mall.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XueWanglin
 * @ClassName GoodsController
 * @Description TODO
 * @Date 2021/1/6 15:39
 * @Version 1.0
 */
@Api(tags = "商品相关操作")
@RestController
public class GoodsController {
   @Autowired
   private GoodsService goodsService;
    /**
     * 查询商品列表
     * @return
     */
    @ApiOperation(value = "分页获取商品信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "查询的页码",required = true ),
            @ApiImplicitParam(name = "pagesize",value = "每一页包含数据条数",required = true)
    })
    @RequestMapping(value = "shangcheng/goodslist",method = RequestMethod.GET)
    public ActionResult getGoodsList(Integer page,Integer pagesize){
//        System.out.println(page);
        List<Goods> list = goodsService.getGoodsListByPage(page,pagesize);
//        System.out.println(list);
        if (list!=null){
            return new ActionResult(200,"ok",list);
        }else {
            return new ActionResult(404,"找不到数据",null);
        }
    }

    /**
     * 通过任一字段查询一个商品信息
     * @return
     */
    @ApiOperation(value = "查询单个商品信息")
    @ApiImplicitParam(name = "goods",value = "{goodsname:华为P30,\n\tgoodsnumber: HW168,\n\tgoodsprice: 4200,}三者任一即可",required = true)
    @RequestMapping(value = "shangcheng/goodsquery",method = RequestMethod.POST)
    public ActionResult getOneByAnyColumn(@RequestBody Goods goods){
//        System.out.println(goods.getGoodsname());
        Goods goods1 = goodsService.getOneByAnyColumn(goods);
        if (goods1!=null){
            return new ActionResult(200,"success",goods1);
        }else {
            return new ActionResult(404,"fail",null);
        }
    }

    /**
     * 通过对象添加商品信息
     * @return
     */
    @ApiOperation(value = "添加商品信息")
    @ApiImplicitParam(name = "goods",value = "没有商品id的对象（json）",required = true)
    @AdminLimit(required = true)
    @RequestMapping(value = "shangcheng/goodsadd",method = RequestMethod.POST)
    public ActionResult addGoodsByExample(@RequestBody Goods goods){
//        System.out.println(goods.toString());
        Goods goods1 = goodsService.addGoodsByExample(goods);
        if (goods1!=null){
            return new ActionResult(200,"success",goods1);
        }else {
            return new ActionResult(404,"fail",null);
        }
    }

    /**
     * 通过id删除商品信息
     * @return
     */
    @ApiOperation(value = "删除商品")
    @ApiImplicitParam(name = "id",value = "要删除目标商品的id",required = true)
    @RequestMapping(value = "shangcheng/goodsdel",method = RequestMethod.DELETE)
    public ActionResult delGoodsById(int id){
        boolean result = goodsService.delGoodsById(id);
        if (result){
            return new ActionResult(200,"success",null);
        }else {
            return new ActionResult(404,"fail",null);
        }
    }

    /**
     * 通过对象更改商品信息
     * @return
     */
    @ApiOperation(value = "更新商品内容")
    @ApiImplicitParam(name = "goods",value = "更改后的内容（不能更改商品id）",required = true)
    @RequestMapping(value = "shangcheng/goodsupdate",method = RequestMethod.PUT)
    public ActionResult updateGoodsByExample(@RequestBody Goods goods){
        boolean result = goodsService.updateGoodsByExample(goods);
        if (result){
            return new ActionResult(200,"success",null);
        }else {
            return new ActionResult(404,"fail",null);
        }
    }

    /**
     * 批量删除品牌
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除商品")
    @ApiImplicitParam(name = "ids",value = "要删除的商品id数组",required = true)
    @RequestMapping(value = "shangcheng/goodslistdel",method = RequestMethod.DELETE)
    public ActionResult delGoodsListById(@RequestParam("ids") Integer[] ids){
        boolean result = goodsService.delGoodsListById(ids);
        if (result){
            return new ActionResult(200,"success",null);
        }else {
            return new ActionResult(404,"fail",null);
        }
    }
}
